package com.issoft.studentresitor.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.issoft.studentresitor.domain.dto.DromDTO;
import com.issoft.studentresitor.domain.pojo.Dorm;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.pojo.Student;
import com.issoft.studentresitor.domain.query.DormQuery;
import com.issoft.studentresitor.domain.vo.DormVo;
import com.issoft.studentresitor.mapper.StudentMapper;
import com.issoft.studentresitor.service.IDormService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Slf4j
@Api(tags = "宿舍管理")
@RestController
@RequestMapping("/admin/dorm")
public class DormController {
    @Autowired
    private IDormService dormService;
    @Autowired
    private StudentMapper studentMapper;
    @ApiOperation("统计信息")
    @GetMapping("/count")
    public Result count() {
        log.info("统计楼栋术 房间数 入住数 剩余数量");
        DormVo dormCountVo = dormService.countDrom();
        return Result.success(dormCountVo);
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page( @RequestParam(required = false) Integer dormId,
                        @RequestParam(required = false) String dormBuilding,
                        @RequestParam(required = false) String roomNumber,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize) {
        DormQuery query = new DormQuery();
        query.setDormId(dormId);
        query.setDormBuilding(dormBuilding);
        query.setRoomNumber(roomNumber);
        query.setPage(page);
        query.setPageSize(pageSize);
        PageResult<DormVo> result = dormService.queryDormPage(query);
        return Result.success(result);
    }
    /**
     * 添加宿舍
     *
     * @return
     */
    @ApiOperation("添加宿舍")
    @PostMapping
    public Result addDorm(@RequestBody DromDTO dormDto) {
        Dorm dorm = new Dorm();
        BeanUtil.copyProperties(dormDto, dorm);
        boolean save = dormService.save(dorm);
        return save ? Result.success("添加成功") : Result.error("添加失败");
    }
    /**
     * 修改宿舍
     *
     * @return
     */
    @ApiOperation("修改宿舍")
    @PutMapping
    public Result updateDorm(@RequestBody DromDTO dormDto) {
        Dorm dorm = new Dorm();
        BeanUtil.copyProperties(dormDto, dorm);
        boolean update = dormService.updateById(dorm);
        return update ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 批量删除宿舍
     *
     * @return
     */
    @ApiOperation("批量删除宿舍")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids) {
        boolean b = dormService.removeByIds(ids);
        return b ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
    /**
     * 根据房间id查询宿舍信息
     *
     * @return
     */
    @ApiOperation("根据房间id查询宿舍信息")
    @GetMapping("/{dromId}")
    public Result queryDormByRoomId(@PathVariable Integer dromId) {
        Dorm dorm = dormService.getById(dromId);
        DormVo dormVo = new DormVo();
        BeanUtil.copyProperties(dorm, dormVo);
        return dorm != null ? Result.success(dormVo) : Result.error("宿舍不存在");
    }
    /**
     * 根据房间id查询所住学生信息
     *
     * @return
     */
    @ApiOperation("根据房间id查询所住学生信息")
    @GetMapping("/queryStuByDromId")
    public Result queryStuByDromId(Integer dromId) {
        List<Student> studentVoList = studentMapper.queryStuByDromId(dromId);
        return studentVoList != null ? Result.success(studentVoList) : Result.error("没有该宿舍信息");
    }
    //将学生拉人到本宿舍
    @ApiOperation("将学生拉到本宿舍")
    @PostMapping("/pullStudent")
    @Transactional
    public Result pullStudent(@RequestParam Integer dormId, @RequestParam Integer id) {
        // 更新学生的宿舍ID
        int update = studentMapper.update(null, new UpdateWrapper<Student>().set("dorm_id", dormId).eq("id", id));
        if (update==1) {
            log.info("更新学生宿舍信息成功");
            // 更新宿舍的入住人数
            dormService.update().setSql("current_occupancy = current_occupancy + 1").eq("dorm_id", dormId).update();
            return Result.success("学生入住成功");
        } else {
            return Result.error("更新学生宿舍信息失败");
        }
    }
}
